package QQclient;


import QQcommon.Massage_2;
import QQcommon.Message;

import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

public class ClientThread extends Thread{
    private Socket socket;

    public ClientThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        while (true) {
            try {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                System.out.println("等待从服务端读取数据");
                Message mes = (Message) ois.readObject();//如果一直接收不到，会一直堵在这里
                //如果读到的是服务端返回的在线用户列表
                if (mes.getMassageType().equals(Massage_2.MEAASGE_RET_ONLINE_FRIEND)){
                    String[] str=mes.getContent().split(" ");
                    System.out.println("\n====当前在线列表====");
                    for (int i = 0; i <str.length ; i++) {
                        System.out.println("在线用户："+str[i]);
                    }

                } else if (mes.getMassageType().equals(Massage_2.MESSAGE_COMM_MES)) {
                    System.out.println(mes.getSender()+" 对 "+mes.getGetter()+" 说 "+mes.getContent());

                } else if (mes.getMassageType().equals(Massage_2.MESSAGE_TO_ALL_MES)) {
                    System.out.println(mes.getSender()+" 对大家说 "+mes.getContent());

                } else if (mes.getMassageType().equals(Massage_2.MESSAGE_SEND_FILE)) {
                    System.out.println(mes.getSender()+" 对 "+mes.getGetter()+" 发送了 "+mes.getFileBytes());
                    FileOutputStream  oos=new FileOutputStream(mes.getDest());
                    oos.write(mes.getFileBytes());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public Socket getSocket() {
        return socket;
    }
}
